﻿using Acme.BookStore.ProductLabelss;
using Acme.BookStore.ReserveInformations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.PolymerizationGoodss
{
    /// <summary>
    ///聚合商品接口实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class IPolymerizationGoodsServices : ApplicationService, PolymerizationGoodsServices
    {
        /// <summary>
        /// 注入
        /// </summary>
        private readonly IRepository<PolymerizationGoods> repository;
        private readonly IWebHostEnvironment env;
        /// <summary>
        ///构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="env"></param>
        public IPolymerizationGoodsServices(IRepository<PolymerizationGoods> repository, IWebHostEnvironment env)
        {
            this.repository = repository;
            this.env = env;
        }

        /// <summary>
        ///聚合商品删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns> 
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("DeletePolymerizationGoods")]
        public async Task<ApiResult> DeletePolymerizationGoods(Guid Id)
        {
            await repository.DeleteAsync(x => x.Id == Id);
            return ApiResult.Ok();
        }

        /// <summary>
        /// 聚合商品显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("GetPolymerizationGoods")]
        public async Task<ApiResult> GetPolymerizationGoods(PolymerizationGoodsDTO dto)
        {
            var list = await repository.GetListAsync();
            if(!string.IsNullOrEmpty(dto.PolymerizationGoodsName))
            {
                list=list.Where(x=>x.PolymerizationGoodsName.Contains(dto.PolymerizationGoodsName)).ToList();
            }
            return ApiResult.Ok(list);
        }

        /// <summary>
        ///聚合商品添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("PostPolymerizationGoods")]
        public async Task<ApiResult> PostPolymerizationGoods(PolymerizationGoodsDTO dto)
        {
            var list = ObjectMapper.Map<PolymerizationGoodsDTO, PolymerizationGoods>(dto);
            await repository.InsertAsync(list);
            return ApiResult.Ok(data: list);
        }

        /// <summary>
        ///聚合商品修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("PutPolymerizationGoods")]
        public async Task<ApiResult> PutPolymerizationGoods(PolymerizationGoodsUpdateDTO dto)
        {
            var list = await repository.FirstOrDefaultAsync(x => x.Id == dto.Id);
            if (list == null)
            {
                return ApiResult.Fail(msg: "聚合商品不能为空");
            }

            ObjectMapper.Map(dto, list);
            await repository.UpdateAsync(list);
            return ApiResult.Ok(data: list);
        }

        /// <summary>
        ///商品照片上传
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost("PostImags")]
        public string PostImags(IFormFile file)
        {
            var root = env.ContentRootPath;
            var newlist = root + "/wwwroot/Files/" + file.FileName;
            using (FileStream fs = System.IO.File.Create(newlist))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return "https://localhost:44392" + "/Files/" + file.FileName;
        }
    }
}
